/* 
 * File:   prepare.h
 * Author: james
 *
 * Created on 2011年3月28日, 下午2:12
 */

#ifndef PREPARE_H
#define	PREPARE_H

#include "sys_def.h"
#include "log.h"
#include "heart_beat_service.h"
#include "command.h"
#include "message.h"
#include <time.h>
#include <unistd.h>
#include <boost/function.hpp>
#include <boost/pool/object_pool.hpp>
namespace edm
{
    namespace prepare
    {
        #define PREPARE "PREPARE"
        
        #define MEMCACHE_TEMPLATE_SUFFIX  "_temp"
        #define MEMCACHE_SUBJECT_SUFFIX   "_sub"
        #define TEMPLATE_PREFIX           "<F"
        #define TEMPLATE_SUFFIX           ">"
        #define SUBJECT_SUFFIX           "(AD)"
        
        #define MEMCACHE_CONNECT_RETRY  5
        
        #define MAILDROP_DEFAULT_CURRENCY 30
        
        #define MSP_DEFAULT_CURRENCY    9
        
        #define DELIVERY_CLEANER_TIMER  60
        #define DELIVERY_CURRENCY_MAX   60
        #define DELIVERY_RETRY_INTERVAL 30
        #define DELIVERY_RETRY_MAX      2
        #define DELIVERY_BLOCK_INTERVAL (15*60)
        #define DELIVERY_BLOCK_MAX      20

        #define TASK_STATUS_TRIGGER     10

        #define PREPARE_TIMER_TICK      10
        
        typedef edm::network::message_header_reserve_t edm_task_id_t;

        typedef edm::common::heart_beat_service_t<edm_task_id_t>  task_timeout_service_t;
        //! predefine class msp_service_t
        class msp_control_t;

        //! delivery detail type
        struct delivery_t
        {
            enum DSTATE{
                 UNCOOK = 0, 
                 UNCOOK_TEMPERR, 
                 COOKED, 
                 COOKED_TEMPERR, 
                 DELIVERIED, 
                 DELIVERIED_TEMPERR,
                 OPERATOR_DELETED,
            };
            
            //! default construct
            delivery_t(edm_task_id_t tid, msp_control_t* m):
            task_id(tid), msp(m), state(UNCOOK), 
            smtp_high_code(edm::common::SMTP_OK), 
            smtp_low_code(edm::common::MX_S_OK),
            retry(0),next_retry_time(0)
            {}
            
            //! task id
            edm_task_id_t  task_id;
            //! owner msp service
            msp_control_t* msp;
            //! delivery state
            DSTATE         state;
            //! smtp high code
            edm::common::SMTP_HIGH_CODE smtp_high_code;
            //! smtp low  code
            edm::common::SMTP_LOW_CODE smtp_low_code;
            //! retry
            int  retry;
            //! next retry time
            time_t next_retry_time;
            //! from
            std::string    from_name;
            std::string    from_addr;
            //! to
            std::string    recipient;
            //! subject
            std::string    subject;
            //! content top
            std::string    content_top;
            //! content bottom
            std::string    content_bottom;
            //! replace params
            std::vector<std::string> replace;
            //! cooked message
            std::string    cooked_message;
        };
        
        //! define of delivery_list_t
        typedef std::list<delivery_t*> delivery_list_t;
        
        //! delivery memory pool 
        typedef boost::object_pool<delivery_t>  delivery_memory_pool_t;

        //!********* callback functions define ***************************************/
        //! prepare & msp handler for delivery
        typedef boost::function<void(delivery_t*)> delivery_handler_t;
        //! callback for some service handler delivery failed
        typedef boost::function<void(delivery_t*)> callback_error_t;
        //! callback for delivery mimer when template cooked
        typedef boost::function<void(delivery_t*)> callback_cooked_t;
        //! callback for delivery mimer when template missed
        typedef boost::function<void(edm_task_id_t)> callback_template_missed_t;
        //! callback for delivery invoker when invoke a child process for maildrpp
        typedef boost::function<void(pid_t, delivery_t*)> callback_fork_t;
        //! callback for delivery cleaner when clean catch SIGCHILD
        typedef boost::function<void(delivery_t*)> callback_clean_t;
    }
}

#endif	/* PREPARE_H */

